﻿using Atlanchim.Crm.Mui.Commands;
using Atlanchim.Crm.Mui.Helpers;
using FirstFloor.ModernUI.Windows.Controls;
using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using CrmEntity = Lmondeil.Crm.EF;

namespace Atlanchim.Crm.Mui.Pages.Actions
{
    public class PageViewActionModel : AbstractPageViewModel
    {
        /// <summary>
        /// Action de référence
        /// </summary>
        public CrmEntity.Action Action { get; set; }

        /// <summary>
        /// Url d'accès à la page d'édition
        /// </summary>
        public override string EditUrl { get; set; }

        /// <summary>
        /// Chargement
        /// </summary>
        public override void Load()
        {
            // Lecture du fragment
            int idAction = 0;
            int.TryParse(this.DicFragments["IdAction"], out idAction);

            // Chargement
            using (CrmContext context = new CrmContext())
            {
                // Contact
                this.Action = context
                                .Actions
                                .Include("Contact")
                                .Where(action => action.Id == idAction)
                                .FirstOrDefault();
                this.EditUrl = string.Format("/Pages/Actions/PageEditAction.xaml#IdAction={0}&IdContact={1}", idAction, this.Action.Contact.Id);
                this.Complement = string.Format("Contact = {0} {1}", this.Action.Contact.Prenom, this.Action.Contact.Nom);
            }

            // Notification
            OnPropertyChanged("Action");
        }

        /// <summary>
        /// Suppression
        /// </summary>
        protected override void OnDeleteCurrentEntity()
        {
            CrmCrudHelper.DeleteEntityNewContext(this.Action);
        }
    }
}
